<?php
namespace app\index\controller;
use think\Log;
use think\Db;
use think\Exception;
use QL\QueryList;
use QL\Ext\AbsoluteUrl;			// 将相对路径自动处理成绝对路径
use QL\Ext\CurlMulti;			// 多线程爬虫
use QL\Ext\PhantomJs;

class Index
{
    public function index()
    {
  		return view('Index/index');
    }

    // 测试采集开始
    public function testSplider() {
    	
    	// $data = [
    	// 	"no" => 11254,
    	// 	"vd" => "www.baidu.com",
    	// 	"hd" => "www.zerofc.cn",
    	// ];
    	// $res = Db::name('video_target')->insertGetId($data);
    	// var_dump($res);
    	// die;

  //   	$jn = '{'name':'tom','age':'20','addr':'beijing'}';
		// $fan_jn = json_decode($jn,true);
		// var_dump($fan_jn);
		// die;

    	// var_dump(base64_decode('aHR0cDovL3Z1c2VyLmtlcHVjaGluYS5jbi9hcGkvZ2V0X3ZpZGVvX2luZm8ucGhwP0dsb2JhbElEPTE3NjA1NTg='));
    	// die;

    	$baseUrl = "http://video.kepuchina.cn/search/search.php?type=%E8%88%AA%E7%A9%BA%E8%88%AA%E5%A4%A9";
    	// 实例querylist框架类
    	$ql = QueryList::getInstance();	
    	// 注册多线程插件
    	$ql->use([CurlMulti::class]);
    	// $ql->use([CurlMulti::class,PhantomJs::class]);

    	//$count_page = $ql->get($baseUrl)->find('.con_pages_break')->next()->find('a')->text();
    	$rules = [
    		"cover_img" => [".list_img li a img","src"],
    		"duration_time" => [".list_img li a sapn","text"],
    		"title" => [".list_img li a p","text"],
    		"target_url" => [".list_img li a","href"],
    		"no" => [".list_img li a","href","",function($content) {
    			preg_match('/content_(.*)\.shtml/i',$content,$arr);
    			return $arr[1];
    		}],
    	];
    	$ql->rules($rules)->curlMulti($baseUrl)->success(function(QueryList $ql,CurlMulti $curl,$r) use ($baseUrl) {
    		$curr_url = $r['info']['url'];
    		//var_dump();
    		if(strpos($curr_url,'page')===false) {
    			$count_page = $ql->get($baseUrl)->find('.con_pages_break')->next()->find('a')->text();	// 总页数
    			for($i=2;$i<=$count_page;$i++) {
    				Log::write('循环页数'.$i);
    				$curl->add($curr_url."&page=$i");
    			}
    		}
    		$data = $ql->query()->getData();

    		try {
    			$res = Db::name('short_video')->insertAll($data->all());
    			if(empty($res)) {
    				throw new Exception('添加视频信息失败',207);
    			}
    		}catch(\Exception $e) {
    			Log::write($e->getMessage());
    		}
    		
    		// var_dump($data->all());
    		// die;

    		if(count($data->all())>0) {
    			// 开始对详情页中的视频链接进行采集
    			$nql = QueryList::use(CurlMulti::class);
    			$rule = [
    				"video_url" => ['._tdp_box','text'],
    			];
    			$nql->rules($rule)->curlMulti(array_column($data->all(),'target_url'))->success(function(QueryList $nql,CurlMulti $curls,$rs) use ($data) {
    				$details_url = $rs['info']['url'];
    				preg_match('/content_(.*)\.shtml/i',$details_url,$arrs);

    				$insert_data['no'] = $arrs[1];    // 视频的唯一编号
    				//$no = $arrs[1];				

    				$str = $nql->find('#player script')->html();
    				preg_match("/,json:\"(.*?)\",width/i",$str,$res);		// 提取base64编码的字符串
    				$video_urls = base64_decode($res[1]);
    				$hash_url = QueryList::get($video_urls)->getHtml();		// 转码得到链接并请求该地址
    				// 取出标清视频地址
    				preg_match("/\"v\",\"url\":\"(.*?)\"\}/i",$hash_url,$vd);
    				// 取出高清
    				preg_match("/\"v_hd\",\"url\":\"(.*?)\"\}/i",$hash_url,$hd);

    				$insert_data['vd'] = $vd[1];    
    				$insert_data['hd'] = $hd[1];    
    				
    				try {
    					$vdieo_tar = Db::name('video_target')->insertGetId($insert_data);
    					if(empty($vdieo_tar)) {
    						throw new Exception("添加视频地址失败", 208);
    					}
    				}catch(\Exception $e) {
    					Log::write($e->getMessage());
    				}

    				// $nql->use(PhantomJs::class,'F:/setsoft/Dev/phpstudy2018/PHPTutorial/WWW/study-project/spliderman/phantomjs_2.1.1/bin/phantomjs.exe');

    				// $data = $nql->browser($rs['info']['url'])->find('#hv_player')->attr('src');
					//print_r($data->all());

    			})->start();

    		}

    		//Log::write($temp);
    		//var_dump($temp);

    	})->start();

    	var_dump('采集完成');
    	die;

    	$url_arr = ["http://www.zerofc.cn"];
    	$rules = [
    		"zongjie" => [".fix .t a","text"],
    		// "zuopin" => ["","text"],
    	];
    	$ql->rules($rules)->curlMulti($url_arr)->success(function(QueryList $ql, CurlMulti $curl, $r) {
    		$curr_url = $r['info']['url'];
    		$views = $ql->query()->getData()->all();
    		var_dump($views);
    		echo "<br/><br/>".$curr_url;
    		die;
    	})->start();

    }

}
